package com.zhuanzhuan.hero.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuanzhuan.hero.user.consts.NumConsts;
import com.zhuanzhuan.hero.user.entity.HeroUserReliableRecord;
import com.zhuanzhuan.hero.user.enums.RiskControlFieldEnum;
import com.zhuanzhuan.hero.user.enums.StateEnum;
import com.zhuanzhuan.hero.user.mapper.HeroUserReliableRecordMapper;
import com.zhuanzhuan.hero.user.pojo.bo.CreateReliableRecordBO;
import com.zhuanzhuan.hero.user.service.HeroUserReliableRecordService;
import com.zhuanzhuan.hero.user.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * dal Interface:HeroUserReliableRecord
 * @author hanjunjun
 * @date 2024-7-5
 */
@Service
@Slf4j
public class HeroUserReliableRecordServiceImpl extends ServiceImpl<HeroUserReliableRecordMapper, HeroUserReliableRecord>
    implements HeroUserReliableRecordService {

    @Override
    public List<HeroUserReliableRecord> queryByParam(Long fromUid, Long toUid, Integer reliableType, Integer operateType) {
        log.info("queryByParam fromUid={} toUid={}", fromUid, toUid);
        HeroUserReliableRecord query = new HeroUserReliableRecord();
        query.setFromUid(fromUid);
        query.setToUid(toUid);
        query.setReliableType(reliableType);
        query.setOperateType(operateType);

        query.setState(StateEnum.EFFECTIVE.getCode());
        return getBaseMapper().select(query);
    }

    @Override
    public HeroUserReliableRecord insert(CreateReliableRecordBO saveBo) {
        HeroUserReliableRecord save = new HeroUserReliableRecord();
        save.setFromUid(saveBo.getFromUid());
        save.setToUid(saveBo.getToUid());
        save.setReliableType(saveBo.getReliableType());
        save.setUnreliableReason(saveBo.getUnreliableReason());
        save.setUnreliablePics(StringUtil.list2Str(saveBo.getUnreliablePics()));
        save.setOperateType(saveBo.getOperateType());
        save.setState(StateEnum.EFFECTIVE.getCode());
        save.setCreateBy(saveBo.getFromUid());
        save.setUpdateBy(saveBo.getFromUid());

        this.save(save);
        return save;
    }

    @Override
    public boolean unreliableAuthRiskcontrolFail(Long authRecordId, List<String> rejectedFields) {
        log.info("不靠谱认证风控失败 authRecordId={}", authRecordId);
        HeroUserReliableRecord dbRecord = this.queryById(authRecordId);
        if (Objects.isNull(dbRecord)) {
            log.info("认证记录不存在，不处理");
            return true;
        }

        LambdaUpdateWrapper<HeroUserReliableRecord> updateWrapper = new LambdaUpdateWrapper<>();
        if (rejectedFields.contains(RiskControlFieldEnum.UNRELIABLE_REASON.getField())) {
            log.info("不靠谱理由风控失败，删除");
            updateWrapper.set(HeroUserReliableRecord::getUnreliableReason, null);
        }
        if (rejectedFields.contains(RiskControlFieldEnum.UNRELIABLE_PICS.getField())) {
            log.info("不靠谱图片风控失败，删除");
            updateWrapper.set(HeroUserReliableRecord::getUnreliablePics, null);
        }
        updateWrapper.eq(HeroUserReliableRecord::getId, authRecordId)
            .eq(HeroUserReliableRecord::getState, StateEnum.EFFECTIVE.getCode());
        this.update(updateWrapper);

        return true;
    }

    @Override
    public void delById(Long id) {
        LambdaUpdateWrapper<HeroUserReliableRecord> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(HeroUserReliableRecord::getState, StateEnum.IS_DELETED.getCode())
            .set(HeroUserReliableRecord::getUpdateBy, NumConsts.SYSTEM_CODE)
            .eq(HeroUserReliableRecord::getId, id);
        this.update(updateWrapper);
    }

    @Override
    public HeroUserReliableRecord queryById(Long id) {
        HeroUserReliableRecord query = new HeroUserReliableRecord();
        query.setId(id);
        query.setState(StateEnum.EFFECTIVE.getCode());
        List<HeroUserReliableRecord> list = getBaseMapper().select(query);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(NumConsts.ZERO);
    }
}